package com.mycity.servlets;

import com.mycity.dao.CommentTab;
import com.mycity.dao.PostTab;
import com.mycity.dao.UserTab;
import com.mycity.jsonops.PostOps;
import com.mycity.jsonops.UserOps;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author celikeins
 */
public class Post extends Base {

    public Post(HttpServletRequest request, HttpServletResponse response) {
        super(request, response);
    }

    public void create() throws IOException, ServletException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        System.out.println(request.getParameter("loc_id"));
        String postText = request.getParameter("question");
        String tags = request.getParameter("tags");
        String[] tagArray = tags.split(",");
        String id = request.getParameter("loc_id");
        String name = request.getParameter("nplace");
        String uri = request.getParameter("photo_id");
        Long postType = 1L;
        if (uri != null || uri.isEmpty() || uri.length() < 3) {
            postType = 2L;
            postText += "|" + uri;
        }
        System.out.println(postText);
        System.out.println(tags);
        PostTab newPost = new PostTab(user.getUserId(), null, postType, null, postText, tagArray, Long.valueOf(id), name);

        Long success = PostOps.createPost(newPost);
        if (success != null && success > 0) {
            response.sendRedirect("/MyCityWeb/Post/view/" + success);
        } else {
            response.sendRedirect("/MyCityWeb/addPost.jsp?postFailed=1");
        }
    }

    public void view(Long postId) throws IOException, ServletException {

        response.setContentType("text/html");

        PostTab post = PostOps.getPostById(postId);
        CommentTab[] comments = PostOps.getCommentsOfPost(postId);
        UserTab owner = UserOps.getUserById(post.getUserId());
        Double[]  coords = PostOps.convertToCoordinates(post.getLocId());
        request.setAttribute("post", post);
        request.setAttribute("comments", comments);
        request.setAttribute("owner", owner);
        request.setAttribute("coords", coords);
        
        request.getRequestDispatcher("/viewPost.jsp").include(request, response);

    }

    public void deletePost(Long postId) throws IOException, ServletException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        Boolean success = PostOps.deletePost(postId);
        response.sendRedirect("/MyCityWeb/home.jsp");
    }

    public void comment(Long postId) throws IOException, ServletException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }

        String commentText = request.getParameter("answer");

        CommentTab newCom = new CommentTab(user.getUserId(), postId, commentText, 0l, 0l, "");

        Boolean success = PostOps.createComment(newCom);
        if (success != null && success) {
            response.sendRedirect("/MyCityWeb/Post/view/" + postId);
        } else {
            response.sendRedirect("/MyCityWeb/addPost.jsp?postFailed=1");
        }
    }

    public void deleteComment(Long postId, Long commentId) throws IOException, ServletException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }


        Boolean success = PostOps.deleteComment(commentId);
        response.sendRedirect("/MyCityWeb/Post/view/" + postId);
    }

    public void giveStar(Long postId, Long commentId) throws IOException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        Boolean success = PostOps.giveStar(commentId);

        response.sendRedirect("/MyCityWeb/Post/view/" + postId);

    }

    public void removeStar(Long postId, Long commentId) throws IOException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        Boolean success = PostOps.removeStar(commentId);

        response.sendRedirect("/MyCityWeb/Post/view/" + postId);

    }

    public void giveDiamond(Long postId) throws IOException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        Boolean success = PostOps.giveDiamond(postId, user.getUserId());

        response.sendRedirect("/MyCityWeb/Post/view/" + postId);
    }

    public void removeDiamond(Long postId) throws IOException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        Boolean success = PostOps.removeDiamond(postId, user.getUserId());

        response.sendRedirect("/MyCityWeb/Post/view/" + postId);
    }
}
